home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / daemons / nfs / nfs-serv.2be / nfs-serv / nfs-server-2.2beta16 / logging.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-13  |  4.2 KB  |  187 lines

  1. /*
  2.  * logging    This module handles the logging of requests.
  3.  *
  4.  * TODO:    Merge the two "XXX_log() calls.
  5.  *
  6.  * Authors:    Donald J. Becker, <becker@super.org>
  7.  *        Rick Sladkey, <jrs@world.std.com>
  8.  *        Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  9.  *        Olaf Kirch, <okir@monad.swb.de>
  10.  *
  11.  *        This software maybe be used for any purpose provided
  12.  *        the above copyright notice is retained.  It is supplied
  13.  *        as is, with no warranty expressed or implied.
  14.  */
  15.  
  16. #include "nfsd.h"
  17.  
  18. #ifdef HAVE_SYSLOG_H
  19. #include <syslog.h>
  20. #else
  21. #define LOG_FILE    "/var/tmp/%s.log"
  22. #endif
  23.  
  24. static int  logging = 0;        /* enable/disable DEBUG logs    */
  25. static int  dbg_mask = D_GENERAL;    /* What will be logged        */
  26. static char log_name[256];        /* name of this program        */
  27. static FILE *log_fp = (FILE *)NULL;    /* fp for the log file        */
  28.  
  29. void log_open(progname, foreground)
  30. char    *progname;
  31. int    foreground;
  32. {
  33. #ifdef HAVE_SYSLOG_H
  34.     openlog(progname, LOG_PID | LOG_NDELAY, LOG_DAEMON );
  35.     if (foreground)
  36.         log_fp = stderr;
  37. #else
  38.     if (!foreground) {
  39.         log_fp = stderr;
  40.     } else {
  41.         char path[1024];
  42.  
  43.         sprintf(path, LOG_FILE, progname);
  44.         logfile = path;
  45.         if ((log_fp = fopen(path, "a")) == NULL)
  46.             return;
  47.     }
  48. #endif
  49.     if (log_fp != NULL)
  50.         setbuf(log_fp, (char *) NULL);
  51.  
  52.     sprintf(log_name, "%s[%d]", progname, getpid());
  53. }
  54.  
  55. void toggle_logging(sig)
  56. int    sig;
  57. {
  58.     (void) signal(sig, toggle_logging);
  59.     dprintf(D_GENERAL, "turned off logging\n");
  60.     logging = 1 - logging;
  61.     dprintf(D_GENERAL, "turned on logging\n");
  62. }
  63.  
  64. void enable_logging(kind)
  65. char    *kind;
  66. {
  67.     if ('a' == *kind && !strcmp(kind, "auth"))
  68.         dbg_mask |= D_AUTH;
  69.     else if ('c' == *kind && !strcmp(kind, "call"))
  70.         dbg_mask |= D_CALL;
  71.     else if ('f' == *kind && !strcmp(kind, "fhcache"))
  72.         dbg_mask |= D_FHCACHE;
  73.     else if ('f' == *kind && !strcmp(kind, "fhtrace"))
  74.         dbg_mask |= D_FHTRACE;
  75.     else if ('r' == *kind && !strcmp(kind, "rmtab"))
  76.         dbg_mask |= D_RMTAB;
  77.     else if ('s' == *kind && !strcmp(kind, "stale"))
  78.         dbg_mask |= D_AUTH | D_CALL | D_FHCACHE | D_FHTRACE;
  79.     else if ('u' == *kind && !strcmp(kind, "ugid"))
  80.         dbg_mask |= D_UGID;
  81.     else
  82.         fprintf (stderr, "Invalid debug facility: %s\n", kind);
  83.     logging = 1;
  84. }
  85.  
  86. int logging_enabled(level)
  87. int    level;
  88. {
  89.     return (logging && (level & dbg_mask));
  90. }
  91.  
  92.  
  93. /* Write something to the system logfile. */
  94. #ifdef __STDC__
  95. void dprintf(int kind, const char *fmt, ...)
  96. #else
  97. void dprintf(kind, fmt, va_alist)
  98. int    kind;
  99. char    *fmt;
  100. va_dcl
  101. #endif
  102. {
  103.     char buff[1024];
  104.     va_list args;
  105.     time_t now;
  106.     struct tm *tm;
  107.  
  108.     if (!(kind & (L_FATAL | L_ERROR | L_WARNING))
  109.      && !(logging && (kind & dbg_mask)))
  110.         return;
  111.  
  112. #ifdef __STDC__
  113.     va_start(args, fmt);
  114. #else
  115.     va_start(args);
  116. #endif
  117. #ifdef HAVE_VPRINTF
  118.     vsprintf(buff, fmt, args);
  119. #else
  120.     /* Figure out how to use _doprnt here. */
  121. #endif
  122.     va_end(args);
  123.  
  124. #ifdef HAVE_SYSLOG_H
  125.     if (kind & (L_FATAL | L_ERROR)) {
  126.         (void) syslog(LOG_ERR, "%s", buff);
  127.     } else if (kind & L_WARNING) {
  128.         (void) syslog(LOG_WARNING, "%s", buff);
  129.     } else if (kind & L_NOTICE) {
  130.         (void) syslog(LOG_NOTICE, "%s", buff);
  131.     } else if (log_fp == NULL) {
  132.         (void) syslog(LOG_DEBUG, "%s", buff);
  133.     }
  134. #endif
  135.     if (log_fp != (FILE *) NULL) {
  136.         (void) time(&now);
  137.         tm = localtime(&now);
  138.         fprintf(log_fp, "%s %02d/%02d/%02d %02d:%02d %s",
  139.               log_name, tm->tm_mon + 1, tm->tm_mday, tm->tm_year,
  140.             tm->tm_hour, tm->tm_min, buff);
  141.     }
  142.  
  143.     if (kind & L_FATAL)
  144.         exit(1);
  145. }
  146.  
  147. /* Log an incoming call. */
  148. void log_call(rqstp, xname, arg)
  149. struct svc_req *rqstp;
  150. char *xname;
  151. char *arg;
  152. {
  153.     char buff[1024];
  154.     register char *sp;
  155.     int i;
  156.  
  157.     if (!logging || !(dbg_mask & D_CALL))
  158.         return;
  159.  
  160.     sp = buff;
  161.     sprintf(sp, "%s [%d ", xname, rqstp->rq_cred.oa_flavor);
  162.     sp += strlen(sp);
  163.     if (rqstp->rq_cred.oa_flavor == AUTH_UNIX) {
  164.         struct authunix_parms *unix_cred;
  165.         struct tm *tm;
  166.  
  167.         unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
  168.         tm = localtime(&unix_cred->aup_time);
  169.         sprintf(sp, "%d/%d/%d %02d:%02d:%02d %s %d.%d",
  170.             tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
  171.             tm->tm_hour, tm->tm_min, tm->tm_sec,
  172.             unix_cred->aup_machname,
  173.             unix_cred->aup_uid,
  174.             unix_cred->aup_gid);
  175.         sp += strlen(sp);
  176.         if ((int) unix_cred->aup_len > 0) {
  177.             sprintf(sp, "+%d", unix_cred->aup_gids[0]);
  178.             sp += strlen(sp);
  179.             for (i = 1; i < unix_cred->aup_len; i++) {
  180.                 sprintf(sp, ",%d", unix_cred->aup_gids[i]);
  181.                 sp += strlen(sp);
  182.             }
  183.         }
  184.     }
  185.     dprintf(D_CALL, "%s]\n\t%s\n", buff, arg);
  186. }
  187.